package com.ebeijia.controller;

import com.ebeijia.common.FileOperation;
import com.ebeijia.dto.ApplyWaterDto;
import com.ebeijia.mybatis.pojo.NoticeInfo;
import com.ebeijia.myenum.ApplyWaterSteteEnum;
import com.ebeijia.myenum.NoticeInfoTypeEnum;
import com.ebeijia.query.NoticeInfoQuery;
import com.ebeijia.query.Paginator;
import com.ebeijia.service.admin.NoticeInfoService;
import com.ebeijia.util.LoggerUtil;
import com.ebeijia.web.ResponseMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YPJ on 2016/5/18.
 */
@Controller
@RequestMapping("/notice")
public class NoticeInfoController {

    @Resource
    private NoticeInfoService noticeInfoService;

    @RequestMapping("/toNoticeList.htm")
    public ModelAndView toNoticeList() {
        ModelAndView modelAndView = new ModelAndView("notice/noticeList");
        Map<String, Object> map = new HashMap<String, Object>();
        for (NoticeInfoTypeEnum e : NoticeInfoTypeEnum.values()) {
            map.put(e.getCode(), e.getText());
        }
        modelAndView.addObject("type", map);
        return modelAndView;
    }

    @RequestMapping("/noticeList.json")
    @ResponseBody
    public ResponseMessage noticeList(NoticeInfoQuery noticeInfoQuery) {
        Paginator<NoticeInfo> paginator = new Paginator<NoticeInfo>(noticeInfoQuery,
                noticeInfoService.selectAllNotice(noticeInfoQuery), noticeInfoService.selectAllNoticeCount(noticeInfoQuery));
        return ResponseMessage.success(paginator);
    }

    @RequestMapping("delete.json")
    @ResponseBody
    public ResponseMessage delete(String id){
       if( noticeInfoService.deleteByPrimaryKey(id)>0){

           return ResponseMessage.success();


       }
        return ResponseMessage.error("102","系统异常");
    }

    @RequestMapping("/save.htm")
    public ModelAndView save(){
        ModelAndView modelAndView = new ModelAndView("notice/noticeSave");
        Map<String, Object> map = new HashMap<String, Object>();
        for (NoticeInfoTypeEnum e : NoticeInfoTypeEnum.values()) {
            map.put(e.getCode(), e.getText());
        }
        modelAndView.addObject("type", map);
        return modelAndView;
    }
    @RequestMapping("/noticeSave.json")
    @ResponseBody
    public ResponseMessage noticeSave(NoticeInfo noticeInfo) throws Exception {
        if(noticeInfoService.insertNoticeInfo(noticeInfo)>0){
            return ResponseMessage.success();
        }
        return  ResponseMessage.error("102");
    }

    @RequestMapping("/noticeEdit.htm")
    public ModelAndView toEdit(String id) throws Exception {
        ModelAndView modelAndView = new ModelAndView("notice/noticeEdit");
        Map<String, Object> map = new HashMap<String, Object>();
        for (NoticeInfoTypeEnum e : NoticeInfoTypeEnum.values()) {
            map.put(e.getCode(), e.getText());
        }
       NoticeInfo noticeInfo =  noticeInfoService.selectByPrimaryKey(id);
        modelAndView.addObject("type", map);
        modelAndView.addObject("notice",noticeInfo);
        return modelAndView;
    }
    @RequestMapping("/noticeEdit.json")
    @ResponseBody
    public ResponseMessage updateNotice(NoticeInfo noticeInfo) throws Exception {
      if(noticeInfoService.updateByPrimaryKey(noticeInfo)>0){
          return ResponseMessage.success("修改成功");
      }
        return ResponseMessage.error("102");
    }

    @RequestMapping("/release.json")
    @ResponseBody
    public ResponseMessage releaseNotice(String id){
        if(noticeInfoService.updateByPrimaryKeySelective(id)>0){
            return ResponseMessage.success();
        }else {
            return ResponseMessage.error("系统异常");
        }
    }

}
